package com.cskaoyan.day9;

public class StuentDemo {
    public static void main(String[] args) {
        for (int i = 0; i < 15; i++) {
            Student studentInstance = Student.getStudentInstance(i + "");
            if (studentInstance != null) {
                System.out.println(studentInstance.toString());
            }
        }
    }
}

class Student {
    private static int count = 0;
    String name;

    private Student() {
    }

    public Student(String name) {
        this.name = name;
    }

    public static Student getStudentInstance(String name) {
        if (count < 10) {
            count++;
            return new Student(name);
        }
        System.out.println("已经创建了10个对象，无法再创建了");
        return null;
    }

    public static Student getStudentInstance() {
        if (count <= 10) {
            count++;
            return new Student();
        }
        System.out.println("已经创建了10个对象，无法再创建了");
        return null;
    }

    public String getName() {

        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
}
